package com.wuqing.mybatisredisdemo.controller;


import com.wuqing.mybatisredisdemo.bo.StudentDetailInfoBO;
import com.wuqing.mybatisredisdemo.entity.Student;
import com.wuqing.mybatisredisdemo.mapper.StudentMapper;
import com.wuqing.mybatisredisdemo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 卢文昊
 * @since 2021-08-02
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;


    @RequestMapping("/queryAll")
    public List<Student> queryAll(){
        return studentService.queryAll();
    }


    @RequestMapping("/queryLikeName")
    public List<Student> queryByName(String name){
        return studentService.queryLikeName(name);
    }

    @RequestMapping("/add")
    public String add(@RequestBody Student student){
        if(! Objects.isNull(student.getId())){
            return "请传入正确的参数";
        }

        return studentService.saveStuInfo(student);
    }

    @RequestMapping("/update")
    public String update(@RequestBody Student student){
        if(student.getId() == null && student.getId() < 1){
            return "请传入正确的参数！";
        }

        return studentService.updateStuInfo(student);
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        if(id == null && id < 1){
            return "请传入正确的参数！";
        }

        return studentService.removeById(id);
    }

    @RequestMapping("/getStudentDetailInfo")
    public List<StudentDetailInfoBO> getStudentDetailInfos(Integer id){
        return studentService.getStudentByIdWithClassInfo(id);
    }
}

